using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using MediaInfoLib;

namespace MovieRenamer
{
	public class MovieFile
	{
		private ArrayList m_matchedMovies = new ArrayList();
		
		public bool Rename { get; set; }
		public string FullFileName { get; set; }
		public string FileName { get; set; }
		public MatchedMovie Movie { get; set; }
		public string MediaType { get; set; }
		public string DestFileName { get; set; }
		public string SearchString { get; set; }
		
		public ArrayList MatchedMovies
		{
			get { return m_matchedMovies; }
			set { m_matchedMovies = value; }
		}
		
		public MovieFile(string fullFileName)
		{
			FullFileName = fullFileName;
			FileName = Path.GetFileName(fullFileName);
			MediaType = "BDRip";
			Rename = true;
			SearchString = Utils.GetSearchString(FileName);
			
			GenerateDestName();
		}
		
		
		public string FullDestFileName
		{
			get
			{
				return Path.Combine(Path.GetDirectoryName(FullFileName), DestFileName);
			}
		}
		
		public void GenerateDestName()
		{
			if (Movie != null)
			{
				DestFileName = string.Format("{0}({1})({2})", Movie.TranslatedTitle, Movie.Title, Movie.Year);
			}
			else
			{
				DestFileName = "%TranslatedTitle%(%Title%)(%Year%)";
			}
			DestFileName += "." + MediaType;
			
			MediaInfo mediaInfo = new MediaInfo();
			mediaInfo.Open(FullFileName);
			
			int videoCount = mediaInfo.Count_Get(StreamKind.Video);
			if (videoCount > 0)
			{
				string height = mediaInfo.Get(StreamKind.Video, 0, "Height");
				string width = mediaInfo.Get(StreamKind.Video, 0, "Width");
				string scanType = mediaInfo.Get(StreamKind.Video, 0, "ScanType");
				
				scanType = "Interlaced".Equals(scanType) ? "i" : "p";
				
				if ("720".Equals(height) || "1 280".Equals(width) || "1280".Equals(width)) {
					DestFileName += ".720" + scanType;
				} else if ("1 080".Equals(height) || "1080".Equals(height) || "1 920".Equals(width) || "1920".Equals(width)) {
					DestFileName += ".1080" + scanType;
				}
								
				DestFileName += "." + Utils.GetReplacement(mediaInfo.Get(StreamKind.Video, 0, "CodecID"));
			}

			int audioCount = mediaInfo.Count_Get(StreamKind.Audio);
			if (audioCount > 0)
			{
				Dictionary<string, string> audio = new Dictionary<string, string>();
				for (int i = 0; i < audioCount; i++)
				{
					string codec = mediaInfo.Get(StreamKind.Audio, i, "CodecID/Hint");
					if (string.IsNullOrEmpty(codec))
					{
						codec = mediaInfo.Get(StreamKind.Audio, i, "CodecID");
					}
					
					codec = Utils.GetReplacement(codec);
					
					string lang =  Utils.GetReplacement(mediaInfo.Get(StreamKind.Audio, i, "Language"));
					
					string languages;
					audio.TryGetValue(codec, out languages);
					
					if (string.IsNullOrEmpty(languages) || !languages.Contains(lang))
					{
						if (!string.IsNullOrEmpty(languages))
						{
							if (!lang.Equals("rus"))
							{
								languages += ".";
							} else
							{
								languages = "." + languages;
							}
						}

						if (!lang.Equals("rus"))
						{
							audio[codec] = languages + lang;
						} else{
							audio[codec] = lang + languages;
						}
					}
				}
				
				foreach (KeyValuePair<string, string> pair in audio)
				{
					DestFileName += "." + pair.Key;
					if (!string.IsNullOrEmpty(pair.Value))
					{
						DestFileName += "." + pair.Value;
					}
				}
			}
			
			int subCount = mediaInfo.Count_Get(StreamKind.Text);
			if (subCount > 0)
			{
				string subLanguages = string.Empty;
				for (int i = 0; i < subCount; i++)
				{
					string subLang = Utils.GetReplacement(mediaInfo.Get(StreamKind.Text, i, "Language"));
					if (!subLanguages.Contains(subLang))
					{
						if (subLanguages.Length > 0)
						{
							if (!subLang.Equals("rus"))
							{
								subLanguages += ".";
							} else
							{
								subLanguages = "." + subLanguages;
							}
						}
						if (!subLang.Equals("rus"))
						{
							subLanguages += subLang;
						} else
						{
							subLanguages = subLang + subLanguages;
						}
					}
				}
				DestFileName += ".SUB";
				if (!string.IsNullOrEmpty(subLanguages))
				{
					DestFileName += "." + subLanguages;
				}
			}
			
			DestFileName += Path.GetExtension(FileName);
		}
	}
}
